/*
 *  Copyright (c) 2009, Baturu.COM. All rights reserved.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.baturu.oss.facade;

import com.baturu.oss.dao.OpenDAOManager;
import com.baturu.oss.utils.OpenConstants;

/**
 * Manages Site options.
 * 
 * @author javafuns
 */
public class OpenSiteOptionFacade {
    private static OpenSiteOptionFacade instance = new OpenSiteOptionFacade();
    private String siteName = "Open Source Site";
    private String siteDesc = "On the Open Source Java Road ...";

    private OpenSiteOptionFacade() {
        String name = this.getValue(OpenConstants.OPEN_OPTION_SITE_NAME);
        if (name != null && !"".equals(name.trim())) {
            siteName = name;
        }
        String desc = this.getValue(OpenConstants.OPEN_OPTION_SITE_DESCRIPTION);
        if (desc != null && !"".equals(desc.trim())) {
            siteDesc = desc;
        }
    }

    public static OpenSiteOptionFacade getInstance() {
        return instance;
    }

    public String getSiteName() {
        return siteName;
    }

    public String getSiteDescription() {
        return siteDesc;
    }

    /**
     * get option value that should be of type int.
     * 
     * @param key
     * @return
     */
    public int getIntValue(String key) {
        return Integer.parseInt(getValue(key));
    }

    /**
     * get option value.
     * @param key
     * @return
     */
    public String getValue(String key) {
        OpenDAOManager odm = OpenDAOManager.newInstance();
        odm.startTransaction();
        try {
            String value = odm.getOptionDAO().getOption(key);
            odm.commitTransaction();
            return value;
        } catch (Exception e) {
            odm.rollback();
            return "";
        } finally {
            odm.close();
        }
    }

    /**
     * refresh cache.
     */
    public void refresh() {
        instance = new OpenSiteOptionFacade();
    }

}
